<?php

/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Widget.php 5400 2010-09-12 17:53:28Z huuphuoc $
 * @since		2.0.9
 */
class News_Widgets_Category_Widget extends Tomato_Widget {

    protected function _prepareShow() {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        
        $pageIndex = $request->getParam('page_index', 1);
        $perPage = 20;
        $offset = ($pageIndex - 1) * $perPage;
       
        
        $articleModel = Rabbit_Model_Factory::getInstance()->setModule('news')->getArticleModel();
        $articles = $articleModel->find($offset, $perPage, array());
        $numArticles = $articleModel->count(array());
        /**
         * Paginator
         */
        $paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($articles, $numArticles));
        $paginator->setCurrentPageNumber($pageIndex);
        $paginator->setItemCountPerPage($perPage);
        $this->_view->assign('articles', $articles);
        $this->_view->assign('numArticles', $numArticles);
        $this->_view->assign('paginator', $paginator);
        $this->_view->assign('paginatorOptions', array(
            'path' => $this->_view->url(array('page_index' => $pageIndex), 'news_index_index'),
            'itemLink' => 'page-%d',
        ));
    }

    protected function _prepareConfig() {
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();
        $categoryModel = Rabbit_Model_Factory::getInstance()->setModule('category')->getCategoryModel();
        $categoryModel->setDbConnection($conn);
        $categories = $categoryModel->getTree();

        $this->_view->assign('categories', $categories);
    }

}
